//
//  UpdateDZViewController.m
//  YDD
//
//  Created by 欧洪飞 on 2017/8/22.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "UpdateDZViewController.h"

#import "MapSelPlaceViewController.h"
@interface UpdateDZViewController ()<MapSelPlaceViewControllerDelegate>
@property (weak, nonatomic) IBOutlet UITextField *textName;
@property (weak, nonatomic) IBOutlet UIButton *btnMan;
@property (weak, nonatomic) IBOutlet UIButton *btnWoman;
@property (weak, nonatomic) IBOutlet UITextField *textPhone;
@property (weak, nonatomic) IBOutlet UILabel *labPlace;
@property (weak, nonatomic) IBOutlet UITextField *textMen;
@property (nonatomic,copy) NSString *sex;


@property (nonatomic, copy) NSString *addressLongitude;

@property (nonatomic, copy) NSString *addressLatitude;

@property (nonatomic, copy) NSString *addressProvince;

@property (nonatomic, copy) NSString *addressCity;

@property (nonatomic, copy) NSString *addressDistric;

@end

@implementation UpdateDZViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadNav];
    
    _btnMan.layer.borderWidth = 0.5;
    _btnMan.layer.cornerRadius = 5.0;
    _btnMan.layer.borderColor = [UIColor grayColor].CGColor;
    
    _btnWoman.layer.borderWidth = 0.5;
    _btnWoman.layer.cornerRadius = 5.0;
    _btnWoman.layer.borderColor = [UIColor grayColor].CGColor;
    _sex = @"2";
    
    //NSLog(@"dic:%@",_dic);
    _textName.text = [_dic objectForKey:@"consignee"];
    _textPhone.text = [_dic objectForKey:@"userphone"];
    NSString *sex = [NSString stringWithFormat:@"%li",[[_dic objectForKey:@"sex"] integerValue]];
    if ([sex integerValue] == 2) {
        _btnMan.backgroundColor = [UIColor redColor];
        [_btnMan setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
        _btnWoman.backgroundColor = [UIColor whiteColor];
        [_btnWoman setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    }else{
        _btnMan.backgroundColor = [UIColor whiteColor];
        [_btnMan setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        
        _btnWoman.backgroundColor = [UIColor redColor];
        [_btnWoman setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }
    _labPlace.text = [NSString stringWithFormat:@"%@%@%@",[_dic objectForKey:@"province"],[_dic objectForKey:@"city"],[_dic objectForKey:@"area"]];
    _textMen.text = [_dic objectForKey:@"detailedadd"];
    
    self.addressProvince = [self.dic objectForKey:@"province"];
    self.addressCity = [self.dic objectForKey:@"city"];
    self.addressDistric = [self.dic objectForKey:@"area"];
    self.addressLongitude = [self.dic objectForKey:@"longitude"];
    self.addressLatitude = [self.dic objectForKey:@"latitude"];
    
    UITapGestureRecognizer *regionGest = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(regionEvent)];
    self.labPlace.userInteractionEnabled = YES;
    [self.labPlace addGestureRecognizer:regionGest];
    
}

- (void)regionEvent{
    MapSelPlaceViewController *map = [[MapSelPlaceViewController alloc] init];
    map.delegate = self;
    [self.navigationController pushViewController:map animated:YES];
}

-(void)sendText:(NSString *)text Province:(NSString *)proce City:(NSString *)city Area:(NSString *)area{
    _labPlace.text = text;
    self.addressProvince = proce;
    self.addressCity = city;
    self.addressDistric = area;
}

-(void)sendLat:(NSString *)lat sendLng:(NSString *)lng{
    self.addressLatitude = lat;
    self.addressLongitude = lng;
}

-(void)sendAddress:(NSString *)address Det:(NSString *)det{
    
}


- (void)loadNav{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 64)];
    view.backgroundColor = [UIColor whiteColor];
    view.userInteractionEnabled = YES;
    [self.view addSubview:view];
    
    UILabel *lab = [[UILabel alloc] init];
    lab.text = @"修改地址";
    lab.textColor = [UIColor blackColor];
    lab.font = [UIFont systemFontOfSize:18.0];
    [view addSubview:lab];
    [lab makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(view.centerX);
        make.top.equalTo(34);
    }];
    
    UILabel *line = [[UILabel alloc] init];
    line.backgroundColor = [UIColor lightGrayColor];
    [view addSubview:line];
    [line makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(0);
        make.right.equalTo(0);
        make.bottom.equalTo(view.bottom);
        make.height.equalTo(0.5);
    }];
    
    //返回按钮
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:[UIImage imageNamed:@"箭头"] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:@"箭头"] forState:UIControlStateHighlighted];
    [btn addTarget:self action:@selector(clickBack) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:btn];
    [btn makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(lab.centerY);
        make.left.equalTo(10);
        make.width.equalTo(30);
        make.height.equalTo(30);
    }];
    
    //删除按钮
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    //btn2.frame = CGRectMake(ScreenWidth-40, 35, 30, 34);
    [btn2 setImage:[UIImage imageNamed:@"删除"] forState:UIControlStateNormal];
    [btn2 addTarget:self action:@selector(clickDel) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:btn2];
    [btn2 makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(lab.centerY);
        make.right.equalTo(-10);
        make.width.equalTo(36);
        make.height.equalTo(39);
    }];
    
}
// 返回
- (void)clickBack{
    [self.navigationController popViewControllerAnimated:YES];
}
//delete删除收货地址
- (void)clickDel{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定删除该地址?" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [SVProgressHUD show];
        [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
        NSString *str = [NSString stringWithFormat:@"%@890d69e502d1d37a9e84335ccefece57",[user objectForKey:@"token2"]];
        NSString *token = [LCMD5Tool md5:str];
        NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
        [parmas setObject:token forKey:@"verifyToken"];
        [parmas setObject:[user objectForKey:@"userid"] forKey:@"userid"];
        [parmas setObject:_addressid forKey:@"addid"];//地址
        NSLog(@"parmas:%@",parmas);
        NSString *url = [NSString stringWithFormat:@"%@/App/Usercenter/deleDeliveryAddress",urlHead];
        [[AFHTTPSessionManager manager] POST:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
            NSLog(@"删除收货地址res:%@",responseObject);
            NSInteger status = [[responseObject objectForKey:@"status"] integerValue];
            NSString *msg = [responseObject objectForKey:@"msg"];
            if (status == 200) {
                [SVProgressHUD showSuccessWithStatus:msg];
                [self.delegate reloadAddressList];
                [self.navigationController popViewControllerAnimated:YES];
            }else{
                [SVProgressHUD showErrorWithStatus:msg];
            }
            
        } failure:^(NSURLSessionDataTask *task, NSError *error) {
            NSLog(@"error:%@",error);
            [SVProgressHUD dismiss];
        }];
        
    }];
    
    [alert addAction:action1];
    [alert addAction:action2];
    [self presentViewController:alert animated:YES completion:nil];
}

- (IBAction)clickMan:(id)sender {
    _btnMan.backgroundColor = [UIColor redColor];
    [_btnMan setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    _btnWoman.backgroundColor = [UIColor whiteColor];
    [_btnWoman setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    _sex = @"2";
}

- (IBAction)clickWoman:(id)sender {
    _btnMan.backgroundColor = [UIColor whiteColor];
    [_btnMan setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    
    _btnWoman.backgroundColor = [UIColor redColor];
    [_btnWoman setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _sex = @"3";
}
//编辑收货地址
- (IBAction)clickUpdate:(id)sender {
    
    
    [SVProgressHUD show];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    NSString *str = [NSString stringWithFormat:@"%@890d69e502d1d37a9e84335ccefece57",[user objectForKey:@"token2"]];
    NSString *token = [LCMD5Tool md5:str];
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    [parmas setObject:token forKey:@"verifyToken"];
    [parmas setObject:[user objectForKey:@"userid"] forKey:@"userid"];
    [parmas setObject:_addressid forKey:@"id"];
    [parmas setObject:_textName.text forKey:@"consignee"];
    [parmas setObject:_sex forKey:@"sex"];
    [parmas setObject:_textPhone.text forKey:@"userphone"];
    [parmas setObject:self.addressProvince forKey:@"province"];
    [parmas setObject:self.addressCity forKey:@"city"];
    [parmas setObject:self.addressDistric forKey:@"area"];
    [parmas setObject:_textMen.text forKey:@"detailedadd"];
    [parmas setObject:self.addressLongitude forKey:@"longitude"];
    [parmas setObject:self.addressLatitude forKey:@"latitude"];
    NSLog(@"parmas:%@",parmas);
    NSString *url = [NSString stringWithFormat:@"%@/App/Usercenter/editUserAddress",urlHead];
    [[AFHTTPSessionManager manager] POST:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"编辑收货地址res:%@",responseObject);
        NSInteger status = [[responseObject objectForKey:@"status"] integerValue];
        NSString *msg = [responseObject objectForKey:@"msg"];
        if (status == 200) {
            [SVProgressHUD showSuccessWithStatus:msg];
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [SVProgressHUD showErrorWithStatus:msg];
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
        [SVProgressHUD dismiss];
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
